package cn.xd.base.domain.entity;

import cn.hutool.core.util.IdUtil;
import cn.xd.base.basic.type.*;
import cn.xd.base.domain.bean.command.AddKeyValueCommand;
import cn.xd.base.domain.bean.command.UpdateKeyValueCommand;
import cn.xd.core.ddd.entity.Entity;
import lombok.Data;

import java.util.Date;


/**
 * @author : mirzaLin
 * @data : 2023/11/20
 * @description : 字典
 **/
@Data
public class KeyValue implements Entity<KeyValueId> {
    private KeyValueId id;
    /**
     * 父节点id
     */
    private KeyValueId parentId;
    /**
     * 路径
     */
    private TreePath path;
    /**
     * 树形结构的层级
     */
    private TreeLevel level;
    /**
     * 类型
     */
    private DictCode dictCode;
    /**
     * 名称
     */
    private KeyValueValue dictValue;
    /**
     * 内容
     */
    private KeyValueKey dictKey;
    /**
     * 是否展示
     */
    private Display display;
    /**
     * 备注
     */
    private Remark remark;

    private UserId creatorId;

    private TimeValue createTime;

    private UserId updaterId;

    private TimeValue updateTime;

    /**
     * 新增字典数据
     *
     * @param command 新增字典数据内容上级节点层级
     * @param userId  操作用户id
     * @return 新增字典数据实体
     */
    public static KeyValue add(AddKeyValueCommand command, Long userId) {
        long id = IdUtil.getSnowflakeNextId();
        Date now = new Date();
        KeyValue keyValue = new KeyValue();
        keyValue.id = KeyValueId.of(id);
        keyValue.dictCode = DictCode.of(command.getDictCode());

        keyValue.parentId = KeyValueId.of(command.getParentId());
        keyValue.dictValue = KeyValueValue.of(command.getDictValue());
        keyValue.dictKey = KeyValueKey.of(command.getDictKey());
        keyValue.display = Display.of(command.getDisplay());
        keyValue.remark = Remark.of(command.getRemark());
        keyValue.creatorId = UserId.of(userId);
        keyValue.createTime = TimeValue.of(now);
        keyValue.updaterId = UserId.of(userId);
        keyValue.updateTime = TimeValue.of(now);
        return keyValue;
    }

    /**
     * 更新字典配置
     *
     * @return 新增字典数据实体
     */
    public static KeyValue updateKeyValue(String type) {
        KeyValue keyValue = new KeyValue();
        keyValue.dictCode = DictCode.of(type);
        keyValue.updateTime = TimeValue.now();
        return keyValue;
    }

    /**
     * 更新字典数据
     *
     * @param command 字典数据更新内容
     * @param userId  操作用户id
     */
    public void update(UpdateKeyValueCommand command, Long userId) {
        this.dictCode = DictCode.of(command.getDictCode());
        this.parentId = KeyValueId.of(command.getParentId());
        this.dictValue = KeyValueValue.of(command.getDictValue());
        this.dictKey = KeyValueKey.of(command.getDictKey());
        this.display = Display.of(command.getDisplay());
        this.remark = Remark.of(command.getRemark());
        this.updaterId = UserId.of(userId);
        this.updateTime = TimeValue.now();
    }

    public void updateDictValue(String value) {
        this.dictValue = KeyValueValue.of(value);
    }


    /**
     * 更新字典数据类型
     *
     * @param type   字典数据更新内容
     * @param userId 操作用户id
     */
    public void update(String type, Long userId) {
        this.dictCode = DictCode.of(type);
        this.updaterId = UserId.of(userId);
        this.updateTime = TimeValue.now();
    }
}
